package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class ajxWhoAdoptedSolutionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
		UserService userService = UserServiceFactory.getUserService(); 
		String UserEmail = userService.getCurrentUser().getEmail().toLowerCase(); 
		
		//Long sol_id = Long.valueOf(req.getParameter("sol_id"));
		
		
		
		float TotalAdtSavings = 0;
		float TotalAdtDays = 0;
		float TotalAchieved = 0;
		 
		SolutionConnect SOL = new SolutionConnect();
		
		
		List<HashMap<String,Object>> sRS = SOL.getQueryShared("submittedBY", UserEmail);
		if ( sRS == null ){
			out.println("<H1>NO SOLUTION SUBMITTED.</H1>");
		}else{
			
			out.println("<TABLE><TBODY>");

			
			for(int i = 0 ; i < sRS.size() ;i++){
				
			
				out.println("<TR><TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 12px; FONT-WEIGHT: bold\" width=120 colSpan=3><U>Shared Solution</U></TD></TR>");
				out.println("<TR>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Solution ID</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Customer</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Site</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Process</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Benefit</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Achieved</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Total Annual Production</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Time To Implement</TD>");
				out.println("</TR>");
				
				out.println("<TR>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100><A href='/modules/default/display-solution.jsp?sol_id=" + sRS.get(i).get("solID") +"' >" + sRS.get(i).get("solCode") + "</A></TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("Customer") + "</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("Location") + "</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("processArea") + "</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("achievedTypeDesc") + "</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("achievedValue") + "</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("annProdSavings") + " $USD</TD>");
				out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + sRS.get(i).get("impDays") + " days</TD>");
				out.println("</TR>");

				out.println("<TR><TD></TD></TR>");
				
				
				Long lSolID = (Long) sRS.get(i).get("solID");
				AdoptionConnect ADT = new AdoptionConnect();
				List<HashMap<String,Object>> rs = ADT.getQuery("solID", lSolID);
				if ( rs != null ){
					
					for(int j = 0 ; j < rs.size() ;j++){
						
					
						if( j == 0 ){
							out.println("<TR><TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 12px; FONT-WEIGHT: bold\" colSpan=3><U>Adopted Solution</U></TD>");
							out.println("</TR><TR><TD width=150></TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Customer</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Site</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Process</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Benefit</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Achieved</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Total Annual Production</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Time To Implement</TD>");
							out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px; FONT-WEIGHT: bold\" bgColor=#e6e6e8 width=100>Time Saved</TD>");
							out.println("</TR>");
						}

						out.println("<TR><TD width=150></TD>");
						
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100><A href='/modules/default/display-adoption.jsp?adopt_id=" + rs.get(j).get("adoptID") +"' >" + rs.get(j).get("Customer") +"</A></TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("Location") + "</TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("processArea") + "</TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("achievedTypeDesc") + "</TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("achievedValue") + "</TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("annProdSavings") + " $USD</TD>");
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + rs.get(j).get("impDays") + " days</TD>");
						String _impDaysAdop  =	(rs.get(j).get("impDays").toString().equals("") ? "0" : rs.get(j).get("impDays").toString());
						String _impDaysOfSub  = (sRS.get(i).get("impDays").toString().equals("") ? "0" : sRS.get(i).get("impDays").toString());
						String _achievedValue =  (rs.get(j).get("achievedValue").toString().equals("") ? "0" : rs.get(j).get("achievedValue").toString());
						String _annProdSavings = (rs.get(j).get("annProdSavings").toString().equals("") ? "0" : rs.get(j).get("annProdSavings").toString());
						float TimeSaved = Float.parseFloat(_impDaysOfSub)  - Float.parseFloat(_impDaysAdop);
						if (TimeSaved <=0){TimeSaved =0;} //ADDED BY KULPRAPAS - SEP 27,2012 - if TimeSave <= 0 then show 0 instead negative value
						out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 11px\" width=100>" + TimeSaved + "</TD>");
						out.println("</TR>");
						
						TotalAdtSavings = TotalAdtSavings + Float.parseFloat(_annProdSavings);
						TotalAdtDays = TotalAdtDays + Float.parseFloat(_impDaysAdop);
						TotalAchieved = TotalAchieved + Float.parseFloat(_achievedValue);
						
					}//FOR LOOP ADOPT	
					
					out.println("<TR>");
					out.println("<TD width=150></TD>");
					out.println("<TD style=\"FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 12px; FONT-WEIGHT: bold\" bgColor=#ebf1d5 colSpan=4>Total achieved through Adoption: </TD>");
					out.println("<TD bgColor=#ebf1d5 width=100><B>" + TotalAchieved  + "</B></TD>");
					out.println("<TD bgColor=#ebf1d5 width=100><B>" + TotalAdtSavings  + " $USD</B></TD>");
					out.println("<TD bgColor=#ebf1d5 width=100><B>" + TotalAdtDays + " Days</B></TD>");			
					out.println("</TR>");
					
					TotalAdtSavings =  TotalAdtDays = TotalAchieved = 0;
					
				}	//IF ADOPT NO NULL				
			} //FOR SOLUTION					
			out.println("<TR><TD colSpan=8><HR color=green></TD></TR></TBODY></TABLE>");
		}	//IF SOLUTION NO EMPTY		
	} //goGet
}
